import request from '../../../../utils/request'
let isSend = false;
let firstseanum = '';
let firstshowToast = true;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        area:['全部','内地','港台','欧美','日本','韩国'],
        TabCur:0,
        vertical:false,
        mvList:[],     //mv列表
        mvListLength:0,
        int_no_null:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
       this.getAllMvList(this.data.area[this.data.TabCur])
       
       //切换论播放方向
       let swipertype = setInterval(()=>{
           let {vertical} = this.data
           vertical =! vertical
           this.setData({
            vertical
           })
        //    console.log(this.data.vertical)
       },4000)
    },
      
     async getAllMvList(area){
         let {mvList , mvListLength} = this.data
       let res =await request("/mv/all",{area,limit:40,offset:mvListLength})
       
       res.data.forEach((item,index)=>{
         if(item.playCount>10000){
            item.playCount = Math.trunc(item.playCount/10000) + "万"        
         }
       })
       mvList.push(...res.data)
    //    console.log(mvList)
    //    console.log(mvListLength)
       mvListLength = mvList.length
       this.setData({
        mvList,
        mvListLength
       })
    },

    chudi(){
    //   console.log("854594")
    if(this.data._TabCur ===1 ){
      this.searchMv()
    }else{
      this.getAllMvList(this.data.area[this.data.TabCur])
    }
     
    },
    
    //切换类型
    tabSelect(e) {
        let {musicId} = this.data
        this.setData({
          TabCur: e.currentTarget.dataset.id,
          scrollLeft: (e.currentTarget.dataset.id-1)*60,
          mvListLength:0,
          mvList:[],
          
        })
        this.getAllMvList(this.data.area[this.data.TabCur])
      },
   //   前往mv播放页面
      tomvplay(event){
         // console.log()
         let vid = event.currentTarget.dataset.vid
         wx.navigateTo({
            url: '/pages/songPackage/pages/mvplay/mvplay?vid='+vid
          })
      },

      clearSearchContent(){
         console.log("dianji")
         this.setData({
           searchContent: '',
           
         })
       },
         // 表单项内容发生改变的回调
   handleInputChange(event){
      let int_no_null = this.data.int_no_null
   if(event.detail.value){
     int_no_null = true
   }else{
     int_no_null = false
   }
   // console.log(event);
   // 更新searchContent的状态数据
   // console.log(event.detail)
   this.setData({
     searchContent: event.detail.value.trim(),
     int_no_null
   })
    if(isSend){
      return
    }
    isSend = true;
    
    // 函数节流
   setTimeout( () => {
     isSend = false;
   }, 200)
  
 },
  
 async searchMv(){
  let {searchContent} = this.data
  if(!searchContent){
    wx.showToast({
      title: '输入不能为空！',
      icon:'error',
      duration:1000,
      mask:true,
      success:function(){
        firstshowToast = false
      }
    })
  }else{
    if(!firstseanum){
      this.setData({
         mvList:[],
         mvListLength:0
       })
       firstseanum=searchContent
    }else if(firstseanum !==searchContent){
      this.setData({
        mvList:[],
        mvListLength:0
      })
      firstseanum=searchContent
      firstshowToast=true
    }
    console.log(this.data.mvList)
    let {mvList , mvListLength} = this.data
    let res = await request('/cloudsearch',{type:1004,keywords:searchContent,limit:10,offset:mvListLength})
    if(!res.result.mvs){
      if(firstshowToast){
        wx.showToast({
          title: '没有更多了',
          icon:'error',
          duration:1000,
          mask:true,
          success:function(){
            firstshowToast = false
          }
        })
      }
    }
    console.log(res)
    res.result.mvs.forEach((item,index)=>{
      if(item.playCount>10000){
         item.playCount = Math.trunc(item.playCount/10000) + "万"        
      }
    })

    mvList.push(...res.result.mvs)
 //    console.log(mvList)
 //    console.log(mvListLength)
    mvListLength = mvList.length
    this.setData({
     mvList,
     mvListLength
    })
  }

 },
 swiperchange(event){
   // let {TabCur} = this.data
   this.setData({
     _TabCur:event.detail.current
   })
 },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})